package com.example.demofdfs.test2;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

public class LinkTest {

	public static void main(String[] args) {
//		List<String> link = new LinkedList<>();
		List<String> link = new ArrayList<>();
		System.out.println(Arrays.toString(link.toArray()));
		test_1(link);
		link.add("a");
		link.add("b");
		link.add("c");
		link.add("d");
		link.add("e");
		System.out.println(link);
//		String[] s1 = {"1","2","3","4","5","6", "7"};
		String[] s1 = {"1","2","3","4"};
		
		String[] s2 = link.toArray(s1);
		
		System.out.println(Arrays.toString(s1));
		System.out.println(Arrays.toString(s2));
		System.out.println(s1 == s2);
		System.out.println(s1.getClass());
		System.out.println((Object)s1.getClass());
		System.out.println((Object)Object[].class);
		System.out.println(Object[].class);
	}
	

	private static void test_1(List<String> link) {
		link = Collections.synchronizedList(link);
		link.add("a");
		link.add("b");
		link.add("c");
		link.add("d");
		link.add("e");
		
		Thread t1 = new Thread(put(link));
		Thread t2 = new Thread(remove(link));
		Thread t3 = new Thread(foreach(link));
		
		t1.start();
		t2.start();
		t3.start();
	}
	
	public static Runnable put(List<String> list) {
		return ()->{
			while (true) {
				list.add(UUID.randomUUID().toString());
				sleep(150);
			}
		};
	}
	
	public static Runnable remove(List<String> list) {
		return ()-> {
			while (true) {
				if (! list.isEmpty())
					list.remove(0);
				sleep(150);
			}
		};
	}
	
	public static Runnable foreach(List<String> list) {
		return ()-> {
			while (true) {
//				System.out.println(list);
				for (Iterator<String> it = list.iterator(); it.hasNext();) {
					System.out.println(it.next());
				}
					
//				System.out.println(list);
				sleep(150);
			}
		};
	}

	public static void sleep(long millis) {
		try {
			Thread.sleep(millis);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
